<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class LazyMoMoveCommand extends Command
{
    private $updateAll = 1;//覆盖更新
    private $systemTable = [
        //系统项目不允许覆盖
        "User","UserGroups","UserGroupsRole","UserLoginLog"
        ,"Admin","AdminMenu","AdminMenuUser","AdminLog"
        ,"ConfCat","PlatConfig","Role","Rules","RulesCategory"
        ,"WechatCode","WechatReply"

    ];
    private $readyTable = [
        //以下项目已完成配置不允许再覆盖
        "aaaaa","aaaaa","aaaaa","aaaaa","aaaaa","aaaaa","aaaaa"
        ,"aaaaa","aaaaa","aaaaa","aaaaa","aaaaa","aaaaa","aaaaa"
        ,"aaaaa","aaaaa","aaaaa","aaaaa","aaaaa","aaaaa"
    ];

    private $safeMode = 0;//1=安全模式，不会覆盖已有文件

    protected static $defaultName = 'lazy:mo:move';

    protected function configure()
    {
        $this
            ->setDescription('Move Entity to App')
            ->addArgument('en', InputArgument::OPTIONAL, 'Entity Name')
            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);
        $en = $input->getArgument('en');


        if ($en) {
            $io->note(sprintf('You passed an argument: %s', $en));
        }

//        if ($input->getOption('option1')) {
//            // ...
//        }
        $count = 0;
        $entity = $en;
        if ($entity == "all") {

            //全部移动
            $root = str_replace("src\Command","",__DIR__);//E:\www\symfony4\src\Command
            $dirEntity = $root."src\Entity";is_dir($dirEntity) OR mkdir($dirEntity, 0777, true);
            //查找Entity
            $entityList = scandir($dirEntity);
            foreach ($entityList as $item) {
                if ( in_array( $entity , $this->systemTable ) ){
                    continue;
                }
                if ( in_array( $entity , $this->readyTable) && $this->updateAll == 0 ) {
                    //已完成的表如果开启覆盖可以覆盖
                    continue;
                }
                $array = mb_split("\.",$item);
                if ($array[count($array) - 1] != "php") {
                    continue;
                }
                $itemName = $array[0];
                $this->moveEntityToApp($itemName);
                $count++;
            }
        }else{
            //移动单个
            $this->moveEntityToApp($entity);
            $count++;
        }



        $io->success("OK,处理了：$count 项");
        return 200;
    }

    public function moveEntityToApp($entity)
    {
        $root = str_replace("src\Command","",__DIR__);//E:\www\symfony4\src\Command
//        $root = E:\www\symfony4\

        if (preg_match("/(.+)?Category/",$entity) == 1) {//YY-mm-dd HH:ii:ss
            $isCat = "/Category";
            $isCatTwig = "cat_";
        }else{
            $isCat = "";
            $isCatTwig = "";
        }



        //controller
        $from = $root."LazyAdminSrc/"."Controller/Admin";
        $to = $root."src/"."Controller/Admin";is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/".$entity."Controller.php";
        $toFile = $to."/".$entity."Controller.php";
        if ($this->safeMode) {
            if (file_exists($toFile)) {
                print_r($toFile." => 已经存在");
                return false;
            }
        }
        file_put_contents($toFile,file_get_contents($fromFile));

        $from = $root."LazyAdminSrc/"."Controller/AdminApi";
        $to = $root."src/"."Controller/AdminApi";is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/".$entity."AdminApiController.php";
        $toFile = $to."/".$entity."AdminApiController.php";
        file_put_contents($toFile,file_get_contents($fromFile));

        $from = $root."LazyAdminSrc/"."Controller/Api";
        $to = $root."src/"."Controller/Api";is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/Api".$entity."Controller.php";
        $toFile = $to."/Api".$entity."Controller.php";
        file_put_contents($toFile,file_get_contents($fromFile));

//        $from = $root."LazyAdminSrc/"."Controller/AdminCommon";
//        $to = $root."src/"."Controller/AdminCommon";is_dir($to) OR mkdir($to, 0777, true);
//        $fromFile = $from."/".$entity."CommonController.php";
//        $toFile = $to."/".$entity."CommonController.php";
//        file_put_contents($toFile,file_get_contents($fromFile));

        //model
        $from = $root."LazyAdminSrc/"."Model";
        $to = $root."src/"."Model";is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/".$entity."Model.php";
        $toFile = $to."/".$entity."Model.php";
        if ($this->safeMode) {
            if (file_exists($toFile)) {
                print_r($toFile." => 已经存在");
                return false;
            }
        }
        file_put_contents($toFile,file_get_contents($fromFile));
//        $from = $root."LazyAdminSrc/"."Model/Common";
//        $to = $root."src/"."Model/Common";is_dir($to) OR mkdir($to, 0777, true);
//        $fromFile = $from."/".$entity."CommonModel.php";
//        $toFile = $to."/".$entity."CommonModel.php";
//        file_put_contents($toFile,file_get_contents($fromFile));

        //setting
        $from = $root."LazyAdminSrc/"."Setting";
        $to = $root."src/"."Setting";is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/".$entity."Setting.php";
        $toFile = $to."/".$entity."Setting.php";
        if ($this->safeMode) {
            if (file_exists($toFile)) {
                print_r($toFile." => 已经存在");
                return false;
            }
        }
        file_put_contents($toFile,file_get_contents($fromFile));

        //templates

        $from = $root."LazyAdminSrc/"."Twig";
        $to = $root."templates/"."Admin"."/".$entity;is_dir($to) OR mkdir($to, 0777, true);
        $fromFile = $from."/".$entity."/{$isCatTwig}list.php.twig";
        $toFile = $to."/{$isCatTwig}list.php.twig";
        if ($this->safeMode) {
            if (file_exists($toFile)) {
                print_r($toFile." => 已经存在");
                return false;
            }
        }
        file_put_contents($toFile,file_get_contents($fromFile));
        $fromFile = $from."/".$entity."/{$isCatTwig}edit.php.twig";
        $toFile = $to."/{$isCatTwig}edit.php.twig";
        if ($this->safeMode) {
            if (file_exists($toFile)) {
                print_r($toFile." => 已经存在");
                return false;
            }
        }
        file_put_contents($toFile,file_get_contents($fromFile));



    }
}
